package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.City;
import com.wm.comm.Constant;
import com.wm.comm.PinYinUtil;
import com.wm.service.CityService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

public class AdminCity extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminCity.class);
	
	private CityService cityService;

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}


	private List isShowList = new ArrayList();

	
	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void initValues(){
		setIsShowListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
	}


	private long id;					//主键ID
	private String cityName;			//城市名称
	private String cityInitials;		//首字母
	//private String cityAnglic;			//城市的简拼
	//private String cityPhonetic;		//城市名称的全拼
	private String province;			//所属省份
	private String provinceInitials;	//首字母
	//private String provinceAnglic;		//省份的简拼
	//private String provincePhonetic;	//省份名称的全拼
	private int isShow = -1;					//是否显示
	private int orderNum;				//排序字段
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public String getCityInitials() {
		return cityInitials;
	}

	public void setCityInitials(String cityInitials) {
		this.cityInitials = cityInitials;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getProvinceInitials() {
		return provinceInitials;
	}

	public void setProvinceInitials(String provinceInitials) {
		this.provinceInitials = provinceInitials;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_city.action?isShow=" + this.isShow);
		this.setPageList(this.getCityService().findCitysByIsShow(this.isShow,"id", 1, pages));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		City city = new City();
		city.setCityName(this.cityName);
		city.setCityInitials(this.cityInitials.toUpperCase());
		city.setCityAnglic(PinYinUtil.getPinYinHeadChar(this.cityName));
		city.setCityPhonetic(PinYinUtil.getPinYin(this.cityName));
		city.setProvince(this.province);
		city.setProvinceInitials(this.provinceInitials.toUpperCase());
		city.setProvinceAnglic(PinYinUtil.getPinYinHeadChar(this.province));
		city.setProvincePhonetic(PinYinUtil.getPinYin(this.province));
		city.setIsShow(this.isShow);
		city.setOrderNum(this.orderNum);
		city.setOrderTime(new Date());
		city.setAddTime(new Date());
		try {
			this.getCityService().saveCity(city);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		City city = this.getCityService().findCityById(this.id);
		this.setCityName(city.getCityName());
		this.setCityInitials(city.getCityInitials());
		//this.setCityAnglic(city.getCityAnglic());
		//this.setCityPhonetic(city.getCityPhonetic());
		this.setProvince(city.getProvince());
		this.setProvinceInitials(city.getProvinceInitials());
		//this.setProvinceAnglic(city.getProvinceAnglic());
		//this.setProvincePhonetic(city.getProvincePhonetic());
		this.setIsShow(city.getIsShow());
		this.setOrderNum(city.getOrderNum());
		return INPUT;
	}
	
	public String doupdate(){
		//添加商品
		City city = this.getCityService().findCityById(this.id);
		city.setCityName(this.cityName);
		city.setCityInitials(this.cityInitials.toUpperCase());
		city.setCityAnglic(PinYinUtil.getPinYinHeadChar(this.cityName));
		city.setCityPhonetic(PinYinUtil.getPinYin(this.cityName));
		city.setProvince(this.province);
		city.setProvinceInitials(this.provinceInitials.toUpperCase());
		city.setProvinceAnglic(PinYinUtil.getPinYinHeadChar(this.province));
		city.setProvincePhonetic(PinYinUtil.getPinYin(this.province));
		city.setIsShow(this.isShow);
		city.setOrderNum(this.orderNum);
		city.setOrderTime(new Date());
		city.setAddTime(new Date());
		try {
			this.getCityService().updateCity(city);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		City city = this.getCityService().findCityById(this.id);
		city.setOrderTime(new Date());
		try {
			this.getCityService().updateCity(city);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getCityService().removeCityById(id);
		return "index";
	}


	
}
